/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

/**
 *
 * @author LONGBK
 */
public class TownMap {
    /*-------- Image & sprite map duong pho ---------------------------*/

    private Image townIMG;
    private Image hotelIMG;
    private Image nypdIMG;
    private Image gunIMG;
    private Image garbageIMG;
    private Image cars1IMG;
    private Image cars2IMG;
    private Image cars3IMG;
    private Image accidentIMG;
    private Image fumesIMG;
    private Sprite fumes;
    private Sprite hieuung;
    private Sprite accident;
    private Sprite garbage;
    private TiledLayer roof1;
    private TiledLayer roof2;
    private TiledLayer gid;
    private TiledLayer wall;
    private TiledLayer wall1;
    private boolean isInsetRoof;
    private Sprite cars1, cars2, cars3;
    private TiledLayer map;
    private int[][] tiles;
    private int width, height;
    public boolean startAccident;
    public boolean isAccident;
    public int timeFire, timeFumes;
    private boolean catolay, acctola;
    public boolean fum_ap;
    public int nextFrame;
    private int mrLX, mrLY;
    public int viewPortX, viewPortY;
    private Image npcIMG;
    private NPC npc;
    private String[] talk;
    private String[] talk1 = {"Này anh bạn, biết Jim Baruva ở đâu không?", "Hắn hay vào quán bar Black Cat, nhưng muốn nhận ra hắn là ai?","thì anh phải có hồ sơ tuyệt mật về hắn ở nhà kho cảnh sát.", "Làm thế nào để vào được đấy?",
                            "Đây là khu phố hỗn loạn, cơ hội của anh sẽ đến ngay thôi!", "Cơ hội của a đến rồi đấy!"};
    private String[] talk2 = {"Tôi biết anh bạn đang tìm ai?", "Sao ông biết?" , "Hắn hay vào quán bar đó nhưng ở trong đó", "có rất nhiều đàn em  của hắn và cả các băng nhóm đối đầu!",
                              "Tôi làm j để tiếp cận hắn?", "Anh cần phải gây ra một vụ ẩu đả", "Bằng cách nào?", "Tôi sẽ bán cho a thuốc nổ giá 100$"};
    public boolean istalk;
    public boolean talking;
    public boolean endtalk;
    public int talkindex;
    private byte[] text;
    private int fe;
    private boolean isDrawing;
    private int endTextID;
    private int timeWaitForAcc;
    public boolean nextTalk;
    public boolean isLoaded;
    public int savePortX;
    public int savePortY;
    public TownMap() {
    }

    public void loadMap(SunnetGameCanvas gameCanvas) {
        try {
            width = gameCanvas.getWidth();
            height = gameCanvas.getHeight();
            timeFire = -1;
            isLoaded = false;
            tiles = new int[][]{
                        {1, 68, 68, 68, 68, 68, 32, 68, 68, 68, 68, 68, 1, 68, 68, 68, 68, 68, 32, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71},
                        {1, 68, 2, 68, 2, 68, 32, 68, 68, 68, 68, 68, 1, 68, 2, 68, 2, 68, 32, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71},
                        {1, 68, 33, 68, 33, 68, 32, 68, 68, 68, 68, 68, 1, 68, 33, 68, 33, 68, 32, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71},
                        {1, 68, 64, 68, 64, 68, 32, 68, 68, 68, 68, 68, 1, 68, 64, 68, 64, 68, 32, 71, 71, 14, 15, 15, 15, 15, 15, 15, 15, 15, 16, 71},
                        {1, 68, 68, 68, 68, 68, 32, 68, 2, 68, 2, 68, 1, 68, 68, 68, 68, 68, 32, 71, 71, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47, 71},
                        {1, 68, 68, 68, 68, 68, 32, 68, 33, 68, 33, 68, 1, 68, 68, 68, 68, 68, 32, 71, 71, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47, 71},
                        {1, 32, 68, 5, 68, 1, 32, 68, 64, 68, 64, 68, 1, 9, 10, 10, 10, 11, 32, 71, 71, 76, 77, 77, 77, 77, 77, 77, 77, 77, 78, 71},
                        {1, 32, 68, 36, 68, 1, 32, 68, 68, 68, 68, 68, 1, 40, 41, 41, 41, 42, 32, 12, 13, 72, 17, 18, 72, 17, 18, 72, 17, 18, 72, 7},
                        {3, 4, 63, 67, 63, 3, 4, 63, 63, 63, 63, 63, 3, 40, 41, 41, 41, 42, 4, 43, 44, 73, 48, 49, 73, 79, 80, 73, 79, 80, 19, 7},
                        {34, 6, 69, 7, 7, 8, 35, 69, 7, 7, 7, 7, 34, 40, 41, 41, 41, 42, 35, 74, 75, 73, 48, 49, 19, 73, 73, 73, 19, 73, 19, 7},
                        {65, 37, 38, 38, 38, 39, 66, 7, 7, 7, 7, 7, 65, 40, 41, 41, 41, 42, 66, 7, 69, 7, 7, 7, 7, 7, 7, 69, 7, 7, 7, 7},
                        {69, 7, 7, 69, 69, 7, 7, 7, 7, 7, 69, 7, 69, 7, 69, 7, 69, 69, 7, 7, 7, 7, 7, 7, 69, 7, 7, 7, 7, 7, 7, 7},
                        {70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70},
                        {7, 50, 50, 7, 7, 7, 50, 50, 7, 7, 7, 50, 50, 7, 7, 7, 7, 50, 50, 7, 7, 7, 50, 50, 7, 7, 7, 50, 50, 7, 7, 7},
                        {7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {20, 20, 20, 21, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {51, 51, 51, 52, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {22, 55, 22, 23, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 26, 27, 27, 27, 27, 27, 27, 28, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {51, 86, 51, 52, 7, 56, 56, 56, 56, 56, 56, 56, 56, 7, 57, 58, 58, 58, 58, 58, 58, 59, 12, 13, 7, 7, 12, 13, 12, 13, 12, 13},
                        {51, 51, 51, 52, 83, 56, 56, 56, 56, 56, 56, 56, 56, 7, 57, 58, 58, 58, 58, 58, 58, 59, 43, 44, 7, 69, 43, 44, 43, 44, 43, 44},
                        {51, 51, 51, 52, 83, 7, 7, 7, 7, 69, 7, 7, 7, 87, 57, 58, 58, 58, 58, 58, 58, 59, 74, 75, 7, 7, 74, 75, 74, 75, 74, 75},
                        {51, 51, 51, 52, 83, 69, 7, 7, 7, 7, 7, 7, 7, 87, 57, 51, 51, 51, 51, 51, 51, 59, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {51, 51, 22, 23, 83, 7, 7, 7, 69, 7, 7, 7, 69, 87, 88, 89, 89, 89, 89, 89, 89, 90, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {84, 85, 53, 54, 83, 7, 7, 7, 7, 7, 7, 7, 7, 87, 91, 92, 93, 92, 93, 93, 92, 60, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {84, 85, 53, 54, 83, 7, 7, 7, 7, 7, 69, 7, 7, 87, 29, 30, 30, 30, 30, 30, 30, 62, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {84, 85, 53, 54, 83, 7, 7, 7, 7, 7, 7, 7, 7, 87, 29, 61, 61, 61, 61, 61, 61, 62, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {84, 85, 7, 7, 83, 69, 7, 7, 7, 7, 7, 7, 7, 87, 29, 31, 31, 31, 31, 31, 31, 62, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7},
                        {7, 7, 7, 7, 83, 7, 7, 69, 7, 7, 7, 7, 69, 87, 7, 69, 7, 7, 7, 7, 69, 7, 83, 7, 7, 7, 7, 7, 7, 7, 7, 7}
                    };
            townIMG = Image.createImage("/townmap.png");
            map = new TiledLayer(32, 28, townIMG, 16, 16);
            for (int row = 0; row < 28; row++) {
                for (int col = 0; col < 32; col++) {
                    map.setCell(col, row, tiles[row][col]);
                }
            }
            roof1 = new TiledLayer(4, 2, townIMG, 16, 16);
            for (int row = 0; row < 2; row++) {
                for (int col = 0; col < 4; col++) {
                    roof1.setCell(col, row, tiles[row + 15][col]);
                }
            }
            roof2 = new TiledLayer(8, 7, townIMG, 16, 16);
            for (int row = 0; row < 7; row++) {
                for (int col = 0; col < 8; col++) {
                    roof2.setCell(col, row, tiles[row + 17][col + 14]);
                }
            }
            gid = new TiledLayer(10, 3, townIMG, 16, 16);
            for (int row = 0; row < 3; row++) {
                for (int col = 0; col < 10; col++) {
                    if (col == 2 || col == 3) {
                        gid.setCell(col, row, 0);
                    } else {
                        gid.setCell(col, row, tiles[row + 18][col + 22]);
                    }
                }
            }
            wall = new TiledLayer(4, 1, townIMG, 16, 16);
            wall1 = new TiledLayer(2, 1, townIMG, 16, 16);
            for(int row = 0; row < 1; row ++){
                for(int col = 0; col < 4; col ++){                   
                    if(row == 0 && (col == 0 || col == 2 || col == 3)){
                        wall.setCell(col, row, tiles[17][col]);
                    }                                        
                }
            }
            for(int row = 0; row < 1; row ++){
                for(int col = 0; col < 2; col ++){
                    wall1.setCell(col, row, tiles[17][col + 2]);
                }
            }
            fumesIMG = Image.createImage("/khoi.png");
            gunIMG = Image.createImage("/gun.png");
            hotelIMG = Image.createImage("/hotel.png");
            nypdIMG = Image.createImage("/nypd.png");
            garbageIMG = Image.createImage("/garbage.png");
            garbage = new Sprite(garbageIMG, 24, 24);          
            cars1IMG = Image.createImage("/cars1.png");
            cars2IMG = Image.createImage("/cars2.png");            
            cars3IMG = Image.createImage("/cars3.png");
            accidentIMG = Image.createImage("/carrac.png");
            accident = new Sprite(accidentIMG, 111, 44);
            fumes = new Sprite(fumesIMG, 64, 64);
            fumes.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
            fumes.setFrame(0);
            hieuung = new Sprite(gameCanvas.getHieungIMG(), 35, 35);
            hieuung.setFrameSequence(new int[]{0, 1, 2});
            hieuung.setFrame(0);         
            cars3 = new Sprite(cars3IMG, 64, 32);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(gameCanvas.prevStage == 0){
            gameCanvas.getMrL().setPosition(2 * 16, 15 * 16);
            talk = talk1;           
        }
        else if(gameCanvas.prevStage == 2){
            gameCanvas.getMrL().setPosition(15 * 16, 10 * 16);
            talk = talk2;
        }
        garbage.setPosition(3 * 16 - 8, 25 * 16 + 8);
        roof1.setPosition(0, 15 * 16);
        roof2.setPosition(14 * 16, 17 * 16);
        gid.setPosition(22 * 16, 18 * 16);      
        cars3.setPosition(9 * 16, 11 * 16);
        wall.setPosition(0, 17 * 16);
        wall1.setPosition(2 * 16, 22 * 16);
        gameCanvas.getLayerManager().append(roof2);
        gameCanvas.getLayerManager().append(wall1);
        gameCanvas.getLayerManager().append(gameCanvas.getMrL());
        if(gameCanvas.prevStage == 0){           
            accident.setFrameSequence(new int[]{0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10, 11, 12});
            cars1 = new Sprite(cars1IMG, 64, 32);
            cars2 = new Sprite(cars2IMG, 64, 32);
            cars1.setPosition(- 24 * 16, 12 * 16);
            cars2.setPosition(35 * 16, 12 * 16);
            gameCanvas.getLayerManager().append(cars1);
            gameCanvas.getLayerManager().append(cars2);
        }
        if(gameCanvas.prevStage == 2){
            accident.setFrameSequence(new int[]{8, 9, 10, 11, 12});
            accident.setPosition(4 * 16, 12 * 16);
            accident.setFrame(0);
            gameCanvas.getLayerManager().append(accident);
            isAccident = true; // Da xay ra tai nan
        }
        gameCanvas.getLayerManager().append(cars3); 
        gameCanvas.getLayerManager().append(garbage);

        for (int index = 0; index < 6; index++) {         
            if (index == 0) {
                if(NPC.listNPC.size() < 6){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/cop.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 1);
                }
                else{
                    npc = NPC.elementAt(index);
                }
                if(gameCanvas.prevStage == 0){
                    npc.setPosition(4 * 16, 8 * 16);
                    npc.startC = 4;
                    npc.startR = 8;
                }
                else if(gameCanvas.prevStage == 2){
                    npc.setPosition(8 * 16, 11 * 16);
                    npc.startC = 8;
                    npc.startR = 11;
                    npc.stopMove();
                }
            }
            if (index == 1) {
                if(NPC.listNPC.size() < 6)
                    npc = new NPC(npcIMG, 16, 32, 1);
                else
                    npc = NPC.elementAt(index);
                if(gameCanvas.prevStage == 0){
                    npc.setPosition(2 * 16, 8 * 16);
                    npc.startC = 2;
                    npc.startR = 8;
                }
                else if(gameCanvas.prevStage == 2){
                    npc.setPosition(6 * 16, 11 * 16);
                    npc.startC = 6;
                    npc.startR = 11;
                    npc.stopMove();
                }
                npcIMG = null;
            }
            if (index == 2) {
                if(NPC.listNPC.size() < 6){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc3.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 2);
                }
                else{
                    npc = NPC.elementAt(index);
                }
                npc.setPosition(14 * 16, 10 * 16);
                npc.startC = 14;
                npc.startR = 11;
            }
            if (index == 3) {
                if(NPC.listNPC.size() < 6)
                    npc = new NPC(npcIMG, 16, 32, 2);
                else
                    npc = NPC.elementAt(index);
                npc.setPosition(25 * 16, 10 * 16);
                npc.startC = 25;
                npc.startR = 10;
                npcIMG = null;
            }
            if (index == 4) {
                if(NPC.listNPC.size() < 6){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc1.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 3);
                }
                else{
                    npc = NPC.elementAt(index);
                }
                npc.setPosition(18 * 16, 10 * 16);
                npc.startC = 18;
                npc.startR = 11;
                npcIMG = null;
            }
            if (index == 5) {
                if(NPC.listNPC.size() < 6){
                    if(npcIMG == null){
                        try {
                            npcIMG = Image.createImage("/npc2.png");
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                    npc = new NPC(npcIMG, 16, 32, 4);
                }
                else{
                    npc = NPC.elementAt(index);
                }
                npc.setPosition(10 * 16, 23 * 16);
                npc.startC = 10;
                npc.startR = 23;
                npcIMG = null;
            }
            gameCanvas.getLayerManager().append(npc);
        }
        //System.out.println("size: " + NPC.listNPC.size());
        if(gameCanvas.prevStage == 0){
            hieuung.setPosition(3 * 16 - 10, 8 * 16);
        }
        else{
            hieuung.setPosition(22 * 16 - 20, 22 * 16 - 10);
        }
        gameCanvas.getLayerManager().append(hieuung);
        gameCanvas.getLayerManager().append(map);   
        isLoaded = true;
        
    }
    public void getCollides(SunnetGameCanvas gameCanvas) {
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();
        //Thoi diem hien chu noi chuyen
        
        if(gameCanvas.prevStage == 0){
            npc = NPC.elementAt(5);
            if(!talking && !endtalk){
                if(npc.collidesWith(gameCanvas.getMrL(), false)){
                    istalk = true;
                }
                else
                    istalk = false;
            }
        }      
        if(gameCanvas.prevStage == 2){
            npc = NPC.elementAt(3);
            if(!talking && !endtalk){
                if(npc.collidesWith(gameCanvas.getMrL(), false)){
                    istalk = true;
                }
                else
                    istalk = false;
            }
        }
        //---------------------------
        if (mrLY == 26 * 16) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        if(mrLX == 0){
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if(mrLX == 31 * 16 - 4){
            gameCanvas.press6 = false;
            gameCanvas.key6 = false;
        }
        // Collision with hotel
        if ((mrLX >= 0 && mrLX <= 3 * 16 && mrLY == 14 * 16 && !isInsetRoof) || (((mrLX <= 3 * 16 && mrLX >= 2 * 16 - 8) || mrLX < 16) && mrLY == 16 * 16 + 8) || (mrLX >= 2 * 16 - 8 && mrLX < 4 * 16 - 4 && mrLY == 25 * 16)) {
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        if ((mrLX == 3 * 16 && mrLY >= 14 * 16 && mrLY <= 21 * 16) || (mrLX == 16 && ((mrLY >= 21 * 16 && mrLY <= 25 * 16) || (mrLY > 15 * 16 + 8 && mrLY < 16 * 16 + 8)))) {
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        //if ((mrLY == 15 * 16 + 8 && ((mrLX < 16) || (mrLX >= 2 * 16 - 8 && mrLX <= 3 * 16))) || (mrLY == 20 * 16 + 8 && mrLX >= 2 * 16 - 4 && mrLX <= 3 * 16) || (mrLX < 4 * 16 && mrLY == 15 * 16 && isInsetRoof)) {
        if ((mrLY == 15 * 16 + 8 && ((mrLX < 16) || (mrLX > 16 && mrLX <= 3 * 16))) || (mrLY == 20 * 16 + 8 && mrLX > 16 && mrLX <= 3 * 16) || (mrLX < 4 * 16 && mrLY == 15 * 16 && isInsetRoof)) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;

        }
        if ((mrLX == 0 && mrLY >= 14 * 16 && mrLY <= 26 * 16) ||(mrLX == 16 && mrLY > 15 * 16 + 8 && mrLY < 16 * 16 + 8)|| (mrLX == 2 * 16 && mrLY == 24 * 16) || (mrLX == 4 * 16 && mrLY >= 14 * 16 - 8 && mrLY <= 25 * 16)) {
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;           
        }
        //Update layer for wall
        if(mrLX < 4 * 16 && mrLY < 16 * 16 && mrLY >= 14 * 16 + 8){
            gameCanvas.getLayerManager().insert(wall, 0);
        }
        if(mrLX < 4 * 16 && mrLY >= 16 * 16){
            gameCanvas.getLayerManager().remove(wall);           
        }
        //Collison with nypd house
        if ((((mrLX < 2 * 16) || (mrLX >= 5 * 16 - 8 && mrLX < 7 * 16) || (mrLX > 11 * 16 && mrLX <= 19 * 16)) && mrLY == 9 * 16 + 8) || (((mrLX >= 16 && mrLX <= 5 * 16) || (mrLX >= 7 * 16 - 8 && mrLX <= 12 * 16)) && mrLY == 7 * 16 + 8)) {
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }

        if ((mrLX == 7 * 16 && mrLY >= 7 * 16 + 8 && mrLY <= 8 * 16 + 4) || (((mrLX == 7 * 16) || (mrLX == 19 * 16)) && mrLY >= 8 * 16 && mrLY < 9 * 16 + 8) || (mrLY >= 7 * 16 && mrLY <= 9 * 16 + 8 && mrLX == 2 * 16 - 4)) {
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if ((mrLY >= 7 * 16 && mrLY <= 9 * 16 + 8 && mrLX == 4 * 16) || (mrLX == 11 * 16 && mrLY < 9 * 16 + 8 && mrLY >= 7 * 16 + 8)) {
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        // Collision gun house

        if (mrLX >= 19 * 16 && mrLY == 8 * 16 + 8) {
            gameCanvas.press2 = false;
            gameCanvas.key2 = false;
        }
        //Collision with cars
        //oto canh sat
        if (mrLX > 8 * 16 && mrLX < 13 * 16 && mrLY == 11 * 16 + 4) {
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        if (mrLX > 8 * 16 + 8 && mrLX < 13 * 16 && mrLY == 10 * 16) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        if (mrLX == 13 * 16 && mrLY < 11 * 16 + 4 && mrLY >= 10 * 16) {
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if (mrLX == 8 * 16 + 8 && mrLY < 11 * 16 + 4 && mrLY >= 10 * 16) {
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }

        // oto tai nan
        if (mrLX > accident.getX() && mrLX < accident.getX() + 100 && mrLY == accident.getY() + accident.getHeight() - gameCanvas.getMrL().getHeight()) {
            gameCanvas.press2 = false;
            gameCanvas.key2 = false;
        }
        if (mrLX == ((accident.getX() + 96) / 4 + 1) * 4 && mrLY < accident.getY() + accident.getHeight() - gameCanvas.getMrL().getHeight() && mrLY > accident.getY() - 16) {
            gameCanvas.press4 = false;
            gameCanvas.key4 = false;
        }
        if (mrLX == ((accident.getX()) / 4) * 4 && mrLY < accident.getY() + accident.getHeight() - gameCanvas.getMrL().getHeight() && mrLY > accident.getY() - 16) {
            gameCanvas.press6 = false;
            gameCanvas.key6 = false;
        }
        if (mrLX > accident.getX() && mrLX < 13 * 16 && mrLY == accident.getY() - 16) {
            gameCanvas.press8 = false;
            gameCanvas.key8 = false;
        }
        //update layer cars      
        if (mrLX > accident.getX() - 16 && mrLX < 14 * 16 && mrLY < 10 * 16) {
            if (!catolay) {
                gameCanvas.getLayerManager().insert(cars3, 2);
                gameCanvas.getLayerManager().insert(accident, 1);
                catolay = true;
            }
        }
        if (mrLX > accident.getX() - 16 && mrLX < 14 * 16 && mrLY > 10 * 16) {
            if (catolay) {
                gameCanvas.getLayerManager().insert(cars3, 7);
                catolay = false;
            }
        }
        //update layer accident
        if (mrLX > accident.getX() - 16 && mrLX < 14 * 16 && mrLY <= 11 * 16) { //accident.getY() - 16
            if (!acctola) {
                gameCanvas.getLayerManager().insert(accident, 1);               
                acctola = true;
            }
        }
        if (mrLX > accident.getX() - 16 && mrLX < 14 * 16 && mrLY > 11 * 16) {
            if (acctola) {
                gameCanvas.getLayerManager().insert(accident, 6);
                gameCanvas.getLayerManager().insert(cars3, 7);
                acctola = false;
            }
        }
        //-----------------------------------------------------------------------------------//
        if (mrLX == 27 * 16 + 4 && mrLY > 21 * 16) {
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if (mrLX > 27 * 16 + 4 && mrLY == 21 * 16) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        // Collision with yellow house
        if (mrLX == 14 * 16 - 8 && mrLY > 16 * 16 && mrLY <= 25 * 16 + 8) {
            gameCanvas.key6 = false;
            gameCanvas.press6 = false;
        }
        if (mrLX > 14 * 16 - 8 && mrLX < 21 * 16 + 8 && mrLY == 25 * 16 + 8) {
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
        if (mrLX == 21 * 16 + 8 && mrLY > 16 * 16 && mrLY <= 25 * 16 + 8) {
            gameCanvas.key4 = false;
            gameCanvas.press4 = false;
        }
        if (mrLX > 14 * 16 - 8 && mrLX < 21 * 16 + 8 && mrLY == 16 * 16) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }

        //Collison with gid
        if (((mrLX >= 22 * 16 - 8 && mrLX < 24 * 16) || (mrLX >= 26 * 16 - 8)) && mrLY == 19 * 16) {
            gameCanvas.key8 = false;
            gameCanvas.press8 = false;
        }
        if (((mrLX >= 22 * 16 - 8 && mrLX < 24 * 16) || (mrLX >= 26 * 16 - 8)) && mrLY == 19 * 16 + 4) {
            gameCanvas.key2 = false;
            gameCanvas.press2 = false;
        }
       
        // Update Layer for hotel & gid
        if ((mrLX < 4 * 16 && mrLY == 18 * 16) && gameCanvas.prevKey == 8) {
            gameCanvas.getLayerManager().insert(roof1, 0);
            isInsetRoof = true;
        }
        if ((mrLX < 4 * 16 && mrLY == 18 * 16) && gameCanvas.prevKey == 2) {
            isInsetRoof = false;
            gameCanvas.getLayerManager().remove(roof1);
        }
        if (mrLY >= 16 * 16 && mrLY <= 19 * 16 && mrLX >= 21 * 16) {
            gameCanvas.getLayerManager().insert(gid, 0);
        }
        if (mrLY > 19 * 16 && mrLX >= 21 * 16) {
            gameCanvas.getLayerManager().remove(gid);
        }
    }

    public void getAccident(SunnetGameCanvas gameCanvas) {
        cars1.move(16, 0);
        cars2.move(-16, 0);
        if (cars1.collidesWith(cars2, true)) {
            gameCanvas.getLayerManager().remove(cars1);
            gameCanvas.getLayerManager().remove(cars2);
            accident.setPosition(cars1.getX(), cars1.getY());
            gameCanvas.getLayerManager().insert(accident, 4);
            isAccident = true;
        }

    }

    public void npcAction() {
        for (int index = 0; index < 6; index++) {
            //npc = (NPC) person.elementAt(index);
            npc = NPC.elementAt(index);
            if(isAccident){
                if (npc.type == 1) {
                    if(npc.isMove){
                        if (!npc.setFrSeqForKey8) {
                            npc.setFrameSequence(npc.seqFor8);
                            npc.key8 = true;
                            npc.setFrSeqForKey8 = true;
                        }
                        if (npc.isMove) {
                            if (npc.key8) {
                                npc.move(0, 4);
                            }
                            if (npc.key6) {
                                npc.move(4, 0);
                            }
                            npc.nextFrame();
                        }
                        if (npc.getY() == (npc.startR + 3) * 16) {
                            if (!npc.setFrSeqForKey6) {
                                npc.setFrameSequence(npc.seqFor6);
                                npc.key6 = true;
                                npc.key8 = false;
                                npc.setFrSeqForKey6 = true;
                            }
                        }
                        if (npc.getX() == (npc.startC + 4) * 16) {
                            npc.setFrameSequence(new int[]{32});
                            npc.stopMove();
                        }
                    }
                }
            }
            if (npc.type == 2) {
                npc.nextFrame();
            }
            if (npc.type == 3) { 
                if (npc.isMove) {
                    if (npc.key6) {
                        npc.move(2, 0);
                    }
                    if (npc.key4) {
                        npc.move(-2, 0);
                    }
                    npc.nextFrame();
                }
                if (npc.getX() == (npc.startC + 4) * 16) {
                    if (npc.isMove) {
                        npc.stopMove();
                        npc.setFrameSequence(npc.stopSeq);
                    }
                    npc.nextFrame();
                    npc.timeStop++;
                    if (npc.getFrame() == 0 && npc.timeStop >= 100) {
                        npc.isMove = true;
                        npc.setFrSeqForKey6 = false;
                        npc.timeStop = 0;
                        if (!npc.setFrSeqForKey4) {
                            npc.setFrSeqForKey4 = true;
                            npc.setFrameSequence(npc.seqFor4);
                            npc.key4 = true;
                            npc.key6 = false;
                        }
                    }
                }
                if (npc.getX() == (npc.startC - 2) * 16) {
                    if (npc.isMove) {
                        npc.stopMove();
                        npc.setFrameSequence(npc.stopSeq);
                    }             
                    npc.nextFrame();     
                    npc.timeStop++;
                    if (npc.getFrame() == 0 && npc.timeStop >= 100) {
                        npc.timeStop = 0;        
                        npc.isMove = true;
                        npc.setFrSeqForKey4 = false;
                        if (!npc.setFrSeqForKey6) {
                            npc.setFrSeqForKey6 = true;
                            npc.setFrameSequence(npc.seqFor6);
                            npc.key4 = false;
                            npc.key6 = true;
                        }
                    }
                }
            }
            if (npc.type == 4) {            
                if (npc.isMove) {
                    if (npc.key2) {
                        npc.move(0, -2);
                    }
                    if (npc.key8) {
                        npc.move(0, 2);
                    }
                    npc.nextFrame();
                }
                if (npc.getY() == (npc.startR - 4) * 16) {
                    npc.setFrSeqForKey2 = false;
                    npc.timeStop = 0;
                    if (!npc.setFrSeqForKey8) {
                        npc.setFrameSequence(npc.seqFor8);
                        npc.setFrSeqForKey8 = true;
                        npc.key8 = true;
                        npc.key2 = false;
                    }
                }
                if (npc.getY() == (npc.startR + 1) * 16) {
                    if (npc.isMove) {
                        npc.stopMove();
                        npc.setFrameSequence(npc.stopSeq);
                    }
                    
                    if (npc.getFrame() != 7 && !npc.nextFrame) {
                        npc.nextFrame();
                    }
                    if(endtalk){
                        //Xem lai
                        if(npc.getFrame() == 7){
                            npc.timeStop++;
                            npc.nextFrame = true;
                        }
                        if (npc.timeStop >= 100) {
                            npc.prevFrame();
                            if(npc.getFrame() == 0){
                                npc.nextFrame = false;
                                npc.timeStop = 0;
                                npc.isMove = true;
                                npc.setFrSeqForKey8 = false;
                                if (!npc.setFrSeqForKey2) {
                                    npc.setFrameSequence(npc.seqFor2);
                                    npc.setFrSeqForKey2 = true;
                                    npc.key8 = false;
                                    npc.key2 = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public void talkProcess(Graphics g, SunnetGameCanvas gameCanvas){
         if(talking){
            if(nextTalk){
                isDrawing = false;
                nextTalk = false;
            }
            istalk = false;
            g.setColor(55, 77, 91);
            g.fillRect(20, height - 90, 200, 60);
            
            if(gameCanvas.prevStage == 0){
                if(talkindex == 0 || talkindex == 3)
                    gameCanvas.getMatNV().setFrame(4);
                else
                    gameCanvas.getMatNV().setFrame(1);
                if(talkindex == 4){
                    timeWaitForAcc ++;
                    if(timeWaitForAcc >= 100){
                        talking = false;
                        if(!isAccident)
                            startAccident = true;
                    }
                }
            }           
            else{
                if(talkindex == 1 || talkindex == 4 || talkindex == 6){
                    gameCanvas.getMatNV().setFrame(4);
                }
                else
                    gameCanvas.getMatNV().setFrame(2);
                if(talkindex == 7){
                    if(!gameCanvas.updateToBag[13]){
                        gameCanvas.updateToTuido(13);
                    }
                }
            }
            gameCanvas.getMatNV().setPosition(20, height - 85);
            gameCanvas.getMatNV().paint(g);
            gameCanvas.getKhungThoai().setPosition(20, height - 90);
            gameCanvas.getKhungThoai().paint(g);
            if(!isDrawing){
                text = new byte[talk[talkindex].length()];
                Designer.toBytesIndex(talk[talkindex] , text);
                isDrawing = true;
                endTextID = 0;
            }
            else{
                if(endTextID < talk[talkindex].length())
                    endTextID ++;
                Designer.drawString(g, text, 0, endTextID, Designer.FONT_TEXT, Designer.MARGIN_LEFT, false, 2, 75 , height - 85, 145, 50);
            }
            

        }
    }
    public void drawMap(Graphics g, SunnetGameCanvas gameCanvas) {
        mrLX = gameCanvas.getMrL().getX();
        mrLY = gameCanvas.getMrL().getY();   
        if (!startAccident) {
            if (mrLY + height / 2 < 30 * 16 && mrLY - height/2 > 0) {
                viewPortY = mrLY- height / 2;
                savePortY = viewPortY;
            }
            if(mrLX - width / 2 > 0 && (32 * 16 - mrLX > width / 2)){
                viewPortX = mrLX - width / 2;
                savePortX = viewPortX;
            }
        }
        gameCanvas.getLayerManager().setViewWindow(viewPortX, viewPortY, width, height);
        hieuung.nextFrame();
        gameCanvas.getLayerManager().paint(g, 0, 0);      
        if(istalk){
            fe ++;           
            text = new byte[3];
            Designer.toBytesIndex("Nói", text);
            if(fe % 2 == 0)
                Designer.drawString(g,text, 0, 3, Designer.FONT_TEXT, 0, width - 20 , height - 45);
        }
        talkProcess(g, gameCanvas);
        if (isAccident) {
            accident.nextFrame();
            if (accident.getFrame() == 20) {
                accident.setFrameSequence(new int[]{8, 9, 10, 11, 12});
                timeFire = 0;
            }
            if (timeFire >= 5000) {
                startAccident = false;
                gameCanvas.nextScence = true;
                gameCanvas.rh = 0;
                timeFire = -1;     
                talking = true;
                talkindex = 5;
                nextTalk = true;
                viewPortX = savePortX;
                viewPortY = savePortY;
            }
            if (timeFumes >= 3000) {
                fum_ap = true;
                fumes.setPosition(accident.getX() + (accident.getWidth() - fumes.getWidth()) / 2, accident.getY() + accident.getHeight() - fumes.getHeight());
                gameCanvas.getLayerManager().insert(fumes, 0);
                timeFumes = 0;
            }
            if (fum_ap) {
                if (nextFrame % 2 == 0) {
                    fumes.nextFrame();
                }
                fumes.move(0, -4);
                if (fumes.getFrame() == 0) {
                    gameCanvas.getLayerManager().remove(fumes);
                    fum_ap = false;
                }
            }
        }
         
        npcAction();
        g.drawImage(hotelIMG, 4 * 16 - viewPortX, 16 * 16 - viewPortY, 0);
        g.drawImage(nypdIMG, 8 * 16 + 4 - viewPortX, 16 - viewPortY, 0);
        g.drawImage(gunIMG, 23 * 16 - viewPortX, 6 * 16 - 8 - viewPortY, 0);
        g.drawImage(cars1IMG, 28 * 16 - viewPortX, 22 * 16 - viewPortY, 0);
        g.drawImage(cars2IMG, 28 * 16 - viewPortX, 24 * 16 - viewPortY, 0);
        g.drawImage(cars3IMG, 28 * 16 - viewPortX, 26 * 16 - viewPortY, 0);
        if(startAccident){
            viewPortX = 0;
            viewPortY = -height / 16;
            g.fillRect(0, height - gameCanvas.rh, width, gameCanvas.rh);
            g.fillRect(0, 0, width, 2 * gameCanvas.rh);
        }
    }
    public void freeMemory() {
        roof1 = null;
        garbage = null;
        roof2 = null;
        gid = null;
        cars1 = null;
        cars2 = null;
        tiles = null;
        fumes = null;
        map = null;
        garbageIMG = null;
        accidentIMG = null;
        fumesIMG = null;
        townIMG = null;
        hotelIMG = null;
        nypdIMG = null;
        gunIMG = null;
        cars1IMG = null;
        cars2IMG = null;
        cars3IMG = null;
        Runtime.getRuntime().freeMemory();
    }
}
